//
//  CommentsViewAvatarCell.swift
//  
//
//  Created by Ralf on 15/6/14.
//
//

import UIKit

class CommentsViewAvatarCell: TableViewCell
{
    let __avatar_images_view_tag  = 10
    let __alias_label_tag         = 20
    let __created_dates_label_tag = 21

    var aliasLabel:       UILabel!
    var createdDateLabel: UILabel!

    override func awakeFromNib()
    {
        super.awakeFromNib()

        initAvatarImageView()
        initAliasLabel()
        initCreatedDateLabel()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func setData(data: [String:AnyObject])
    {
        let contact         = data["contact"] as! [String:AnyObject]
        let thumbnailURL    = contact["thumbnailURL"] as? String
        let avatarImageView = self.viewWithTag(__avatar_images_view_tag) as? UIImageView

        if thumbnailURL == nil
        {
            avatarImageView!.image = UIImage(named: "Avatar-Default.png")
        }
        else
        {
            let url = appDelegate.parameterManager.server + "/" + thumbnailURL!
            avatarImageView!.load(url, placeholder: nil)
        }

        aliasLabel.text = contact["alias"] as? String

        let createdDate = data["createdDate"] as! String
        let createdate = NSDate.DateFromJSONStringCN(createdDate)
        createdDateLabel.text = createdate.toNormalDate()
    }

    func initAvatarImageView()
    {
        var imageView = self.viewWithTag(__avatar_images_view_tag) as? UIImageView

        if imageView == nil
        {
            imageView = UIImageView()
            imageView!.tag = __avatar_images_view_tag
            self.addSubview(imageView!)
        }
        let frame = CGRect(x: 10.0, y: 5.0, width: 30.0, height: 30.0)
        imageView!.frame = frame
        imageView!.layer.borderColor = UIColor.blackColor().CGColor
        imageView!.layer.borderWidth = 1.0
        imageView!.layer.cornerRadius = 15
        imageView!.layer.masksToBounds = true
    }

    func initAliasLabel()
    {
        if aliasLabel == nil
        {
            aliasLabel = UILabel()
            aliasLabel.tag == __alias_label_tag
            self.addSubview(aliasLabel)
        }

        let frame = CGRect(x: 50.0, y: 5.0, width: 100.0, height: 30.0)
        aliasLabel!.frame = frame
        aliasLabel!.font = UIFont.systemFontOfSize(14)
        aliasLabel!.textColor = UIColor.darkGrayColor()
        aliasLabel!.textAlignment = .Left
    }

    func initCreatedDateLabel()
    {
        if createdDateLabel == nil
        {
            createdDateLabel = UILabel()
            createdDateLabel.tag == __created_dates_label_tag
            self.addSubview(createdDateLabel)
        }
        let frame = CGRect(x: screen.size.width - 130, y: 5.0, width: 120.0, height: 30.0)
        createdDateLabel.frame = frame
        createdDateLabel.font = UIFont.systemFontOfSize(12)
        createdDateLabel.textColor = UIColor.grayColor()
        createdDateLabel.textAlignment = .Right
    }
}
